/**
 * @author a. lothore
 * @classDescription GUIEvents object : API
 */
 
GUIEvents.prototype = {};

//--------------------------------------------------------------------------------------------------------------------------
/*
 * Constructor
*/
function GUIEvents()
{
   this.refreshList   = GUIEvents_refreshList;
   
   //Views switching
   this.switchToView  = GUIEvents_switchToView;
   this.showView	  = GUIEvents_showView;
   this.hideView	  = GUIEvents_hideView;
   
   //List operations
   this.applyEdit     = GUIEvents_applyEdit;
   this.cancelEdit    = GUIEvents_cancelEdit;
   this.insertNew     = GUIEvents_insertNew;
   this.remove	      = GUIEvents_remove;
   
   //Navigation
   this.goToNext		= GUIEvents_goToNext;
   this.goToPrevious	= GUIEvents_goToPrevious;
   this.goToFirst		= GUIEvents_goToFirst;
   this.goToLast		= GUIEvents_goToLast;
}

//--------------------------------------------------------------------------------------------------------------------------
/*
 * Applies edit of a form on js model
 * @obj : object to process (DOM object or GUI object)
 * @onSaveCallback : the callback function on save.
 * @formViewName : the name of the form view
 */
function GUIEvents_applyEdit(params)
{
	if (params)
	{
		var dataObject = GG.get(params.obj);
		if (dataObject)
		{
			dataObject.onSave = params.onSaveCallback || defaultSettings.onSave;
			dataObject.applyEdit(params.formViewName || defaultSettings.formViewName);			
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/*
 * Cancel edit of a form on js model
 * @obj : object to process (DOM object or GUI object)
 * @onCancelCallback : the callback function called after cancelling.
 * @formViewName : the name of the form view
 */
function GUIEvents_cancelEdit(params)
{
	if (params)
	{
		var dataObject = GG.get(params.obj);
		if (dataObject)
		  {
			dataObject.refresh();
			if (params.onCancelCallback)
				{params.onCancelCallback();}
		  }
	}
}


//--------------------------------------------------------------------------------------------------------------------------
/*
 * Delete an object
 * @obj : object to process (DOM object or GUI object)
 */
 function GUIEvents_remove(params)
{
	if (params)
	{
		var dataObject = GG.get(params.obj);
		if (dataObject)
			{dataObject.remove();}
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Refresh a GUIList (reload from database)
 * @obj : object to process (DOM object or GUIList object)
 * @dataType : dataType to search (optionnal)
 */
function GUIEvents_refreshList(params)
{	
  if (params)
  {
	  if (params.dataType) 
		{manualLoad(params);} //TODO : refactor this
	  else
	  {
		var GUIList = GG.api.getGGList(params.obj);
		
		if (!GUIList) 
		{
		  //if no existing GUIList, can't refresh (TODO : will be created ??)
		  $.log("Can't refresh : no GUIlist");

		}
		else
		{
		  //If existing GUIList, refresh it
		  GUIList.ready(function () {
			GUIList.goToView(getNodeAttribute(GUIList.getContainer(""),"defaultView"));});//TODO : this is strange, why not get default view of list ?? 	       
		  
		  //SetTimeout to prevent browser freeze
		  setTimeout(function() {GUIList.loadData();},0);
		}
	  }
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Switch to specific view (applies to a list or an object)
 * @obj : object to process (DOM object, GUI object or GUIList object)
 * @viewName : The view to switch to
 * @hideFunction : optionnal, the function used to hide (e.g. : for animation)
 * @showFunction : optionnal, the function used to show (e.g. : for animation) 
 */
function GUIEvents_switchToView(params)
{
	if (params)
	{
		var dataObject = GG.get(params.obj);
		if (dataObject)
			{dataObject.goToView(params.viewName,params.hideFunction,params.showFunction);}
		else
		{	
			//Not an object, maybe it's a list
			dataObject = GG.getList(params.obj);
			if (dataObject)
				{dataObject.goToView(params.viewName,params.hideFunction,params.showFunction);}
		}		
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Show a specific view (applies to a list or an object)
 * @obj : object to process (DOM object, GUI object or GUIList object)
 * @viewName : The view to show
 * @showFunction : optionnal, the function used to show (e.g. : for animation) 
 */
function GUIEvents_showView(params)
{
	if (params)
	{
		var dataObject = GG.get(params.obj);
		if (dataObject)
			{dataObject.show(params.viewName,params.showFunction);}
		else
		{	
			//Not an object, maybe it's a list
			dataObject = GG.getList(params.obj);
			if (dataObject)
				{dataObject.show(params.viewName,params.showFunction);}
		}		
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Hide a specific view (applies to a list or an object)
 * @obj : object to process (DOM object, GUI object or GUIList object)
 * @viewName : The view to show
 * @hideFunction : optionnal, the function used to hide (e.g. : for animation) 
 */
function GUIEvents_hideView(params)
{
	if (params)
	{
		var dataObject = GG.get(params.obj);
		if (dataObject)
			{dataObject.hide(params.viewName,params.hideFunction);}
		else
		{	
			//Not an object, maybe it's a list
			dataObject = GG.getList(params.obj);
			if (dataObject)
				{dataObject.hide(params.viewName,params.hideFunction);}
		}		
	}
}


//--------------------------------------------------------------------------------------------------------------------------
/* Inserts an empty element
 * @obj : object to process
 * @viewName : the view name to display (if not set, the default view will be used)
 * @position : optional, position of the new view within its container. Can be first (first of list) or last (last of list)
 * @dataType : optional, if specified, element added to "datatype" child
 */
function GUIEvents_insertNew(params)
{
	if (params)
	{
		var dataList;
		if (params.dataType) 
			{dataList = GG.api.getChildGUI(params.obj,params.dataType);}
		else 
			{dataList = GG.api.getGGList(params.obj);}
		if (dataList)
			{
				var _position = params.position||"first";
				dataList.insertEmpty(_position,params.dataType).show(params.viewName);
			}
		else
			GG.api.log("Trying to insert an empty object typed '"+ params.dataType + "' : the dataList can't be found.","E");
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Go to the previous object within list (hide current and show previous)
 * @obj : the DOM object to process (DOM object or GUI object)
 * @viewName : the view name to display
 */
function GUIEvents_goToPrevious(params)
{
	if (params)
	{
		var dataObject = GG.api.getGG(params.obj);
		var previous = GG.api.getPrevious(params.obj);
		if (dataObject && previous)
		{
			dataObject.hide(params.viewName);
			previous.show(params.viewName);
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Go to the next object within list (hide current and show next)
 * @obj : the DOM object to process (DOM object or GUI object)
 * @viewName : the view name to display
 */
function GUIEvents_goToNext(params)
{
	if (params)
	{
		var dataObject = GG.api.getGG(params.obj);
		var next = GG.api.getNext(params.obj);
		if (dataObject && next)
		{
			dataObject.hide(params.viewName);
			next.show(params.viewName);
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Go to the first object within list (hide current and show first)
 * @obj : the DOM object to process (DOM object or GUI object)
 * @viewName : the view name to display
 */
function GUIEvents_goToFirst(params)
{
	if (params)
	{
		var dataObject = GG.api.getGG(params.obj);
		var first = GG.api.getFirst(params.obj);
		if (dataObject && first)
		{
			dataObject.hide(params.viewName);
			first.show(params.viewName);
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------------
/* Go to the last object within list (hide current and show last)
 * @obj : the DOM object to process (DOM object or GUI object)
 * @viewName : the view name to display
 */
function GUIEvents_goToLast(params)
{
	if (params)
	{
		var dataObject = GG.api.getGG(params.obj);
		var last = GG.api.getLast(params.obj);
		if (dataObject && last)
		{
			dataObject.hide(params.viewName);
			last.show(params.viewName);
		}
	}
}